package com.hnl.action.talent;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.ReferralDAO;
import com.hnl.dao.TalentDAO;
import com.hnl.to.Referral;
import com.hnl.to.Talent;
import com.hnl.to.wrapper.ReferralWrapper;
import com.hnl.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class AddReferralInfo extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory
			.getLogger(AddReferralInfo.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {
			Integer talentID = Integer.valueOf(req
					.getParameter(ATTRIBUTE_TALENT_ID));
			Integer referralID = Integer.valueOf(req
					.getParameter(ATTRIBUTE_REFERRAL_ID));
			Boolean hasExisted = false;
			
			String state = req.getParameter(ATTRIBUTE_REFERRAL_EDIT_STATE);
			ReferralDAO referralDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getReferralDAO();
			TalentDAO talentDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getTalentDAO();
			Talent oldTalent=null;
			Referral oldReferral=null;
			// Referral newReferral = new Referral();
			Referral newReferral = referralDAO.loadReferral(referralID,
					talentID);
			if (newReferral == null) {
				newReferral = new Referral();
			} else {
				hasExisted = true;
			}
			newReferral.setReferralId(referralID);
			newReferral.setTalentId(talentID);
			newReferral.setRelationship(req.getParameter("relationship"));
			newReferral.setComment(req.getParameter("referralComment"));
			newReferral.setIsDelete(false);
			if (Boolean
					.parseBoolean(req.getParameter(PARA_REFERRAL_IS_PRIMARY))) {
				newReferral.setIsPrimary(true);
			} else {
				newReferral.setIsPrimary(false);
			}
			if (!hasExisted && state.equals("new")) {
				referralDAO.saveReferral(newReferral);
			} else {
				referralDAO.updateReferral(newReferral);
			}
			
			if(req.getParameter("oldPrimary")!=null){
				Integer primaryTID=Integer.valueOf(req.getParameter("oldPrimary"));
				if(newReferral.getIsPrimary()&&newReferral.getReferralId()!=primaryTID){
					oldReferral=referralDAO.loadReferral(primaryTID,talentID);
					oldReferral.setIsPrimary(false);
					oldReferral.setIsDelete(false);
					referralDAO.updateReferral(oldReferral);
				}
			}
	
			Collection<ReferralWrapper> Referrals = new Vector<ReferralWrapper>();
			Collection<Referral> referralCollection = referralDAO
					.loadReferral(talentID);
			for (Referral referral : referralCollection) {
				oldTalent=talentDAO.loadTalent(referral.getReferralId());
				if(!oldTalent.getIsdelete()){
					ReferralWrapper aReferral = new ReferralWrapper(referral);
					aReferral.getReferralName();
					Referrals.add(aReferral);
				}				
			}
			writer.println(JSONResponseUtil.getJSONObjectString(Referrals));

		} catch (DAOException e) {
			LOG.error("Error to add talent.", e);
		} finally{
			writer.close();
		}

	}
}
